import axios from 'axios';


// 登录请求方法
const loginreq = (method, url, params) => {
    console.log(url);
    console.log(params);
    return axios({
        method: method,
        url: "http://localhost:60281" + url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: params,
        traditional: true,
        transformRequest: [
            function (data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => res.data);
};
// 通用公用方法
const req = (method, url, params) => {
    // console.log("参数"+params);
    return axios({
        method: method,
        // url:" http://localhost:34103"+url,
        url: " http://localhost:60281" + url,

        headers: {
            'Content-Type': 'application/json ;charset=UTF-8',
            // 'Content-Type': 'application/x-www-form-urlencoded',
            // 'Authorization': "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoidV9hZG1pbiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6InJfYWRtaW4iLCJqdGkiOiJhZG1pbiIsIlVzZXJuYW1lIjoiQWRtaW4iLCJOYW1lIjoi6LaF57qn566h55CG5ZGYIiwibmJmIjoxNzMzOTg0ODYzLCJleHAiOjE3MzM5ODQ4OTMsImlzcyI6Ind3dy5iYWlkdS5jb20uY24iLCJhdWQiOiJ3d3cuQXVkaWVuY2UuY29tLmNuIn0.5hc3FQWRN3vvc1sQmQrq3_oYkHc3x7VAgFTkZEHE_9w"
        },
        data: params,
        //data: JSON.stringify(params) ,
        traditional: true,

    }).then(res => res.data);


};
const request = (method, url, params) => {
    // console.log("参数"+params);
    return axios({
        method: method,
        url: " http://localhost:60281" + url,

        headers: { "Content-Type": "multipart/form-data" },
        data: params,
        traditional: true,

    }).then(res => res.data);


};
//请求拦截器
axios.interceptors.request.use(
    config => {

        config.headers.Authorization = "Bearer "+window.localStorage.getItem("token");
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

export {
    loginreq,
    req,
    request
}